﻿using ERPEF.Model;
using ERPIService;
using Microsoft.EntityFrameworkCore;
using S4Project;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERPService
{
    public class bs_dict_imageService : bs_dict_imageIService
    {

        private readonly MyDbContext MyDbContext;
        public bs_dict_imageService(MyDbContext myDb)
        {
            this.MyDbContext = myDb;
        }

        //图片上传
        public Task<int> FUpd(bs_dict_image dict_Image)
        {
            MyDbContext.bs_dict_image.Add(dict_Image);
            return MyDbContext.SaveChangesAsync();
        }
        //配料添加
        public async Task<int> ingredientAdd(bs_dict_image dict_Image)
        {
            if (dict_Image == null)
            {
                throw new ArgumentNullException(nameof(dict_Image));
            }

            // 根据type字段获取当前最大的sort值
            int maxSort = await MyDbContext.bs_dict_image
                .Where(d => d.di_type == dict_Image.di_type)
                .Select(d => (int?)d.di_sort)
                .MaxAsync() ?? 0; // 使用空合并运算符处理没有记录的情况

            // 将新数据的sort值设置为当前最大的sort值加一
            dict_Image.di_sort = maxSort + 1;

            MyDbContext.bs_dict_image.Add(dict_Image);
            return await MyDbContext.SaveChangesAsync();
        }
        //配料逻辑删除
        public async Task<int> ingredientDelete(int di_id)
        {
            var del = MyDbContext.bs_dict_image.Find(di_id);
            if (del != null)
            {
                del.IsDeleted = true;
                await MyDbContext.SaveChangesAsync();
                return 1;
            }
            return 0;
        }
        //配料修改
        public async Task<int> ingredientUpdate(bs_dict_image dict_Image)
        {
            MyDbContext.bs_dict_image.Update(dict_Image);
            return await MyDbContext.SaveChangesAsync();
        }

        //产品添加
        public async Task<int> productsAdd(bs_dict_image dict_Image)
        {

            if (dict_Image == null)
            {
                throw new ArgumentNullException(nameof(dict_Image));
            }

            // 根据type字段获取当前最大的sort值
            int maxSort = await MyDbContext.bs_dict_image
                .Where(d => d.di_type == dict_Image.di_type)
                .Select(d => (int?)d.di_sort)
                .MaxAsync() ?? 0; // 使用空合并运算符处理没有记录的情况

            // 将新数据的sort值设置为当前最大的sort值加一
            dict_Image.di_sort = maxSort + 1;

            MyDbContext.bs_dict_image.Add(dict_Image);
            return await MyDbContext.SaveChangesAsync();
        }
        //产品逻辑删除
        public async Task<int> productsDelete(int di_id)
        {
            var del = MyDbContext.bs_dict_image.Find(di_id);
            if (del != null)
            {
                del.IsDeleted = true;
                await MyDbContext.SaveChangesAsync();
                return 1;
            }
            return 0;
        }
        //产品修改
        public async Task<int> productsUpdate(bs_dict_image dict_Image)
        {
            MyDbContext.bs_dict_image.Update(dict_Image);
            return await MyDbContext.SaveChangesAsync();
        }
        //配料查询
        public async Task<List<bs_dict_image>> Queryingredient()
        {
            return await MyDbContext.bs_dict_image
       .Where(w => w.IsDeleted == false && w.di_type == "配料")
       .ToListAsync();
        }

        //产品查询
        public async Task<List<bs_dict_image>> Queryproducts()
        {
            return await MyDbContext.bs_dict_image
        .Where(w => w.IsDeleted == false && w.di_type == "产品")
        .ToListAsync();
        }
        //包装查询
        public async Task<List<bs_dict_image>> Querywrap()
        {
            return await MyDbContext.bs_dict_image
         .Where(w => w.IsDeleted == false && w.di_type == "包装")
         .ToListAsync();
        }
        //包装添加
        public async Task<int> wrapAdd(bs_dict_image dict_Image)
        {
            if (dict_Image == null)
            {
                throw new ArgumentNullException(nameof(dict_Image));
            }

            // 根据type字段获取当前最大的sort值
            int maxSort = await MyDbContext.bs_dict_image
                .Where(d => d.di_type == dict_Image.di_type)
                .Select(d => (int?)d.di_sort)
                .MaxAsync() ?? 0; // 使用空合并运算符处理没有记录的情况

            // 将新数据的sort值设置为当前最大的sort值加一
            dict_Image.di_sort = maxSort + 1;

            MyDbContext.bs_dict_image.Add(dict_Image);
            return await MyDbContext.SaveChangesAsync();
        }
        //包装逻辑删除
        public async Task<int> wrapDelete(int di_id)
        {
            var del = MyDbContext.bs_dict_image.Find(di_id);
            if (del != null)
            {
                del.IsDeleted = true;
                await MyDbContext.SaveChangesAsync();
                return 1;
            }
            return 0;
        }
        //包装修改
        public async Task<int> wrapUpdate(bs_dict_image dict_Image)
        {
            MyDbContext.bs_dict_image.Update(dict_Image);
            return await MyDbContext.SaveChangesAsync();
        }
    }
}
